define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/add/index' + location.search,
                    add_url: 'goods/add/add',
                    edit_url: 'goods/add/edit',
                    del_url: 'goods/add/del',
                    multi_url: 'goods/add/multi',
                    import_url: 'goods/add/import',
                    table: 'goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                showToggle: false,
                showColumns: false,
                showExport: false,
                searchFormVisible: true,
                search: false,
                clickToSelect: false,
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'id', title: __('ID'), operate: '='},
                        {field: 'cover_pic', title: __('Cover_pic'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {
                            field: 'price',
                            title: __('Price'), operate: false,
                            formatter: function (value) {
                                return value + '元'
                            }},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons:[
                                {
                                    name: '查看',
                                    text: '查看',
                                    classname: 'btn btn-primary btn-dialog',
                                    extend: 'data-area=["100%","100%"]',
                                    url: 'goods/add/detail',
                                },
                                {
                                    name: '添加',
                                    text: __('Add'),
                                    classname: 'btn btn-info btn-ajax',
                                    url: 'goods/add/add',
                                    refresh: true,
                                },
                            ],
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);

                                // 隐藏编辑和删除按钮
                                $(table).data("operate-edit", null);
                                $(table).data("operate-del", null);

                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});